package com.laopan.wxpay.utils;

import com.laopan.wxpay.WxConstants;
import org.apache.http.entity.ContentType;

import java.util.HashMap;
import java.util.Map;

/**
 * http工具类
 *
 * @author laopan
 * @className HttpUtils
 * @date 2021/2/22 13:46
 */
public class HttpUtils {

    private static final String OS = System.getProperty("os.name") + "/" + System.getProperty("os.version");
    private static final String VERSION = System.getProperty("java.version");


    public static ContentType getContentType() {
        return ContentType.create("application/json", "utf-8");
    }

    public static Map<String, String> getWxPayHeaders(String authorization, String serialNo) {
        Map<String, String> headers = getBaseHeaders(authorization);
        if (null != serialNo && 0 != serialNo.trim().length()) {
            headers.put("Wechatpay-Serial", serialNo);
        }
        return headers;
    }

    /**
     * 获取UserAgent.
     *
     * @return
     */
    public static String getUserAgent() {
        return String.format(
                "laopan-wxpay/%s (%s) Java/%s", HttpUtils.class.getPackage().getName(),
                OS,
                VERSION == null ? "Unknown" : VERSION);
    }

    public static Map<String, String> getBaseHeaders(String authorization) {
        Map<String, String> headers = new HashMap<>(5);
        headers.put("Accept", WxConstants.CONTENT_TYPE_JSON);
        headers.put("Authorization", authorization);
        headers.put("User-Agent", getUserAgent());
        headers.put("Content-Type", WxConstants.CONTENT_TYPE_JSON);
        return headers;
    }
}
